#include<iostream>
#include<queue>
using namespace std;

int n, m;
char maze[50][50];
bool vis[50][50];
int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

bool in(int x, int y) {
	return x >= 0 && x < n && y >= 0 && y < m;
}

struct node {
	int x, y, step;
	node (int xxx, int yyy, int ssstep) {
		x = xxx;
		y = yyy;
		step = ssstep;
	}
};

int bfs(int start_x, int start_y) {
	queue<node> q;
	q.push(node(start_x, start_y, 0));
	vis[start_x][start_y] = true;
	while (!q.empty()) {
		node now = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int dx = now.x + dir[i][0];
			int dy = now.y + dir[i][1];
			if (in(dx, dy) && maze[dx][dy] != '*' && !vis[dx][dy]) {
				if (maze[dx][dy] == 'T') {
					return now.step + 1;
				} else {
					vis[dx][dy] = true;
					q.push(node(dx, dy, now.step + 1));
				}
			}
		}
	}
	return -1;
}


int main(void) {
	
	cin >> n >> m;
	
	for (int i = 0; i < n; i ++) {
		for (int j = 0; j < m; j++) {
			cin >> maze[i][j];
		}
	}
	int sx, sy;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (maze[i][j] == 'S') {
				sx = i;
				sy = j;
			}
		}
	}
	cout << "sx = " << sx << "  sy = " << sy << endl;
	cout << bfs(sx, sy) << endl;
	
	return 0;
}

